<template>
  <div class="data-container">
    <div class="tab-list">
      <div class="item" :class="{ active: active === 0 }" @click="handleTab(0)">行业占比</div>
      <div class="item" :class="{ active: active === 1 }" @click="handleTab(1)">行业排名</div>
    </div>
    <dv-decoration-2 style="width: 200px; height: 5px" :dur="10" />
    <div class="tab-panel" v-show="active === 0">
      <p class="text">
        按现有数据
        <i class="el-icon-arrow-down"></i>
      </p>
      <div id="echarts1"></div>
    </div>
    <div class="tab-panel" v-show="active === 1">
      <p class="text">
        按现有数据
        <i class="el-icon-arrow-down"></i>
      </p>
      <div class="rank-list">
        <div class="item-left">
          <div class="item" v-for="(item, index) in dataSort" :key="item.hangye">
            <div class="name">第{{ index + 1 }}名 {{ item.hangye }}</div>
            <div class="line-box">
              <div class="line" :style="{ width: item.width + '%' }"></div>
            </div>
          </div>
        </div>
        <div class="item-right">
          <div class="item" v-for="item in dataSort" :key="item.hangye">
            <p class="total">{{ item.total }}条</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { getIndexLeftData } from '@/api/realTime/index'

export default {
  name: 'panel1',
  data() {
    return {
      active: 0,
      myEcharts: '',
      dataList: [],
      option: {},
      dataSort: [],
    }
  },

  mounted() {
    this.getData()
  },
  methods: {
    onInterval() {
      this.handleTab((this.active + 1) % 2, 'interval')
    },
    // 获取数据
    getData() {
      getIndexLeftData()
        .then((res) => {
          const { update_time, data1 } = res.data
          this.$emit('updateTime', update_time)
          data1.forEach((item) => {
            item.width = 0
          })
          this.dataList = data1
          this.initEcharts()
          this.hanldeDataSort(false)
        })
        .catch(() => {
          this.$emit('stopInterval')
        })
    },
    // 排行数据
    hanldeDataSort(show) {
      if (this.dataList && this.dataList.length > 0) {
        const arr = this.dataList.sort((a, b) => {
          return b.total - a.total
        })
        const max = arr[0].total
        arr.forEach((item) => {
          if (show) {
            item.width = ((item.total / max) * 100).toFixed(2)
          } else {
            item.width = 0
          }
        })
        this.dataSort = arr
      }
    },

    // tab切换
    handleTab(index, type) {
      if (type !== 'interval') {
        this.$emit('pauseInterval')
      }
      this.active = index
      if (this.active === 0) {
        this.$nextTick(() => {
          this.myEcharts.resize()
          this.myEcharts.clear() // 清空画布
          this.myEcharts.setOption(this.option) // 重新绘制
          this.hanldeDataSort(false)
        })
      } else {
        setTimeout(() => {
          this.hanldeDataSort(true)
        }, 200)
      }
    },
    // 初始化图表
    initEcharts() {
      const echartsData = this.dataList.map((item) => {
        return {
          name: item.hangye,
          value: item.total,
        }
      })
      this.myEcharts = echarts.init(document.getElementById('echarts1'))
      this.option = {
        tooltip: {},
        series: [
          {
            type: 'pie',
            radius: '60%',
            center: ['50%', '50%'],
            color: ['#68cee0', 'rgba(104, 206, 224, 0.8)', 'rgba(104, 206, 224, 0.6)', 'rgba(104, 206, 224, 0.4)', 'rgba(104, 206, 224, 0.2)', 'rgba(255, 255, 255, 0.2)', 'rgba(255, 255, 255, 0.4)', 'rgba(255, 255, 255, 0.6)'],
            data: echartsData,
          },
        ],
      }

      // 使用刚指定的配置项和数据显示图表。
      this.myEcharts.setOption(this.option)
    },
    onResize() {
      if (this.myEcharts) {
        this.myEcharts.resize()
      }
    },
  },
}
</script>
<style lang="scss" scoped>
$bgColor: #060a22;
$mainColor: #68cee0;
$mainColorOpcity: rgba(104, 206, 224, 0.3);
.data-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding: 20px;
  overflow: hidden;
}
.tab-list {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .item {
    width: 100px;
    height: 30px;
    border: 1px solid #ffffff;
    border-radius: 3px 0 0 3px;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    &:hover {
      border: 1px solid $mainColor;
      color: $mainColor;
    }
    &.active {
      border: 1px solid $mainColor;
      background: $mainColorOpcity;
      font-weight: bold;
      color: $mainColor;
    }

    &:last-child {
      margin-right: 0;
      border-radius: 0 3px 3px 0;
      border-left: none;
    }
  }
}
#echarts1 {
  width: 100%;
  height: 100%;
}
.tab-panel {
  width: 100%;
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;
  .text {
    width: 100%;
    color: #ffffff;
  }
}

.rank-list {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overflow-y: scroll;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  &::-webkit-scrollbar {
    width: 0;
  }
  .item-left {
    flex: 1 1 auto;
    margin-right: 10px;
  }
  .item-right {
    flex: 0 0 auto;
  }
  .item {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    height: 45px;
    .name {
      color: #ffffff;
      width: 100%;
    }
    .line-box {
      width: 100%;
      margin-top: 6px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .line {
        width: 0;
        height: 10px;
        border-radius: 5px;
        background: $mainColor;
        transition: 1s;
      }
    }
    .total {
      margin: 0;
      margin-top: 18px;
      color: $mainColor;
    }
  }
}
</style>
